| Conditions | 11 |
| Total Lines | 73 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like timeConversion.ts ➔ tryUpdateTime often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import spacetime from 'spacetime'; |
||
| 111 | |||
| 112 | function tryUpdateTime(node: Node): boolean { |
||
| 113 | const REG_TIME = /\d?\d:\d{2}( (AM|PM))?/g; |
||
| 114 | const REG_AMPM = /\s(am|pm)/i; |
||
| 115 | |||
| 116 | let hits = Array.from(node.textContent.matchAll(REG_TIME), match => match[0]); |
||
| 117 | |||
| 118 | if (hits.length === 0) { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | hits.forEach(hit => { |
||
| 123 | let use24Format = false; |
||
| 124 | let processedStr = hit |
||
| 125 | |||
| 126 | // string must be converted into 12h format |
||
| 127 | if (processedStr.search(REG_AMPM) < 0) { |
||
| 128 | let timeStr = processedStr.match(REG_TIME)[0]; |
||
| 129 | let hm = timeStr.split(':'); |
||
| 130 | let hour = parseInt(hm[0]); |
||
| 131 | |||
| 132 | if (hour >= 12) { |
||
| 133 | timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); |
||
| 134 | timeStr += 'pm'; |
||
| 135 | } |
||
| 136 | else { |
||
| 137 | timeStr += 'am'; |
||
| 138 | } |
||
| 139 | |||
| 140 | processedStr = processedStr.replace(REG_TIME, timeStr); |
||
| 141 | use24Format = true; |
||
| 142 | } |
||
| 143 | |||
| 144 | // if time has a space before am/pm, this has to be removed for spacetime |
||
| 145 | processedStr = processedStr.replace(REG_AMPM, '$1'); |
||
| 146 | |||
| 147 | let datetime = spacetime(); |
||
| 148 | datetime = datetime.goto('UTC+1'); |
||
| 149 | datetime = datetime.time(processedStr); |
||
| 150 | datetime = datetime.goto(spacetime().tz); |
||
| 151 | let replaceText = String(datetime.format(getSpaceTimeFormat(use24Format))); |
||
| 152 | |||
| 153 | node.textContent = node.textContent.replace(hit, replaceText); |
||
| 154 | |||
| 155 | // SPECIAL CASE: Anime has the day written in broadcast bade. This may be different in another timezone |
||
| 156 | let tzMeta = spacetime().timezone(); |
||
| 157 | let originalH = datetime.hour() - tzMeta.current.offset + 1; |
||
| 158 | |||
| 159 | let dOffset = 0; |
||
| 160 | // we moved to next day |
||
| 161 | if (originalH < 0) { |
||
| 162 | dOffset = 1; |
||
| 163 | } |
||
| 164 | // we moved to previous day |
||
| 165 | else if (originalH > 24) { |
||
| 166 | dOffset = -1; |
||
| 167 | } |
||
| 168 | |||
| 169 | // if day changed |
||
| 170 | if (dOffset != 0) { |
||
| 171 | let dayNode = (node.parentNode as Element)?.previousElementSibling; |
||
| 172 | if (helper.assigned(dayNode)) { |
||
| 173 | for (let i = 0; i < DAYS.length; i++) { |
||
| 174 | if (dayNode.textContent.indexOf(DAYS[i]) >= 0) { |
||
| 175 | dayNode.textContent = dayNode.textContent.replace(DAYS[i], DAYS[(i + DAYS.length + dOffset) % DAYS.length]); // add DAYS.length to avoid negative numbers in the modulo operation |
||
| 176 | break; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | }); |
||
| 182 | |||
| 183 | return true; |
||
| 184 | } |
||
| 228 | } |